package ext.tianma.doc.util;

import java.io.File;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ext.tianma.util.PropertiesUtil;
/**
 * 获得文档所有状态
 * @author deng
 *
 */
@Controller
@RequestMapping({ "/GetStates" })
public class GetStates {
	private static String configFilePath = "codebase" + File.separator + "ext"
			+ File.separator + "tianma" + File.separator + "cfg"
			+ File.separator + "TianmaPro.properties";
	
	@RequestMapping(value = "/getStates", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void getStates(HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		PropertiesUtil ut = new PropertiesUtil(configFilePath);
		String states = ut.getValueByKey("DCC.states");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			if(!"".equals(states) && states != null){
		        String[] drawing = states.split(";");
		        for (int i = 0; i < drawing.length;i++){
		        	String[] drawingType = drawing[i].split(":");
		        	ob.put("status", "success");
		        	ob.put("message", "查询成功!");
		        	JSONObject obInner = new JSONObject();
		        	obInner.put("researchName", drawingType[1]);
		        	obInner.put("showName", drawingType[0]);
		        	array.put(obInner);
		        }
	        	ob.put("data", array);
			}else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} 
	}
}
